// 根据用户id和班级id，判断用户是否已经加入指定班级
import express from 'express';
import pool from "../importantApi/db_connection.js";

const router = express.Router();
router.post("/", async (req, res) => {
    const {userId, classId} = req.body;
    if (!userId || !classId) {
        return res.status(401).json({
            code: 401,
            msg: "缺少必要信息"
        })
    }
    try {
        let [result] = await pool.query(`
            select count(*)    as count
            from user_class uc
            where uc.user_id = ${userId}
              and uc.class_id = ${classId}
        `)
        if (result[0].count === 0) {
            return res.status(200).json({
                code: 200,
                msg: "未加入该班级",
            })
        } else {
            return res.status(201).json({
                code: 201,
                msg: "已加入该班级",
            })
        }
    } catch (err) {
        console.log(err)
        return res.status(500).json({
            code: 500,
            msg: "查询错误：server error"
        })
    }
})
export default router;